package com.farina.cryptobox;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import com.farina.cryptobox.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
	public final static String EXTRA_MESSAGE_USN = "com.example.myfirstapp.USN";
	public final static String EXTRA_MESSAGE_PWD = "com.example.myfirstapp.PWD";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		EditText pwdText = (EditText) findViewById(R.id.passwdMainField);
		pwdText.setText("");
		
	}
	

	
	protected void onActivityResult(int requestCode, int resultCode,
			Intent data) {
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				Context context = getApplicationContext();
				CharSequence text = "Account Successfully Created!";
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
		}
		if (requestCode == 2) {
			if (resultCode == RESULT_OK) {
				Context context = getApplicationContext();
				CharSequence text = "Account Deleted!";
				int duration = Toast.LENGTH_SHORT;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
		}
	}

	/** Called when the user clicks the Send button */
	public void createNewAccount(View view) {
		Intent intent = new Intent(this, NewAccount.class);
		startActivityForResult(intent, 1);
	}

	public int checkAccountAndPwd(String usn, String pwd){
		
		File f = new File(getExternalFilesDir(null), "accounts.txt");
		FileReader fr = null;
		BufferedReader br = null;
		try{
			fr = new FileReader(f);
			br = new BufferedReader(fr);
			while(true){
				String tmpUsn = br.readLine();
				if(tmpUsn==null){br.close(); return 1;}
				String tmpPwd = br.readLine();
				if(tmpUsn.equals(usn) && tmpPwd.equals(pwd))break;
				if(tmpUsn.equals(usn) && !tmpPwd.equals(pwd)){br.close(); return 2;}
			}

			br.close(); 
		}
		catch (IOException e) {      
			e.printStackTrace();
			return 3;
		}

		return 0;
	}

	public void accessAccount(View view) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		EditText pwdText = (EditText) findViewById(R.id.passwdMainField);
		String pwd = pwdText.getText().toString();
		EditText usnText = (EditText) findViewById(R.id.usnMainField);
		String usn = usnText.getText().toString();

		String pwdHash=CryptoFunction.SHA256(pwd);
		String usnHash=CryptoFunction.MD5(usn);

		int res = checkAccountAndPwd(usnHash, pwdHash);
		if(res==1){return;}
		if(res==2){return;}
		if(res==3){return;}

		Intent intent = new Intent(this, AccountManager.class);
		intent.putExtra(EXTRA_MESSAGE_USN, usn);
		intent.putExtra(EXTRA_MESSAGE_PWD, pwd);
		this.startActivityForResult(intent, 2);
	}

	public void close(View view){
		this.finish();
		
	}


}
